#!/bin/sh /etc/rc.common
# Author=wulishui , 20200908-0915 ; <wulishui@gmail.com> 
# 20210514 by sirpdboy

START=55


up_cron() {
	
	sum=$(grep -c 'config crontab' /etc/config/rebootschedule)
	for i in $(seq 0 $((sum - 1))); do
		enable=$(uci get rebootschedule.@crontab[$i].enable 2>/dev/null)
		if [ "$enable" -eq "1" ]; then
			month=$(uci get rebootschedule.@crontab[$i].month 2>/dev/null) || month="*"
			day=$(uci get rebootschedule.@crontab[$i].day 2>/dev/null) || day="*"
			week=$(uci get rebootschedule.@crontab[$i].week 2>/dev/null) || week="*"
			hour=$(uci get rebootschedule.@crontab[$i].hour 2>/dev/null) || hour="*"
			minute=$(uci get rebootschedule.@crontab[$i].minute 2>/dev/null) || minute="00"
			command=$(uci get rebootschedule.@crontab[$i].command 2>/dev/null) || command="echo "Reboot schedule tested.""
			echo "$minute $hour $day $month $week $command #rebootschedule" >>/etc/crontabs/root
		fi
	done
	/etc/init.d/cron restart
}

del_cron() {
	sed -i '/rebootschedule/d' /etc/crontabs/root 2>/dev/nul
}


start()
{       
        del_cron
	if [ ! -e /etc/config/rebootschedule ]; then
	    logger -p daemon.error -t 'rebootschedule' "missing config /etc/config/rebootschedule!"
	exit 1
	fi
	up_cron
}

stop()
{
    del_cron
}
